﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BuXiaChu.OrderDishes.Model;
using BuXiaChu.OrderDishes.BLL;
using BuXiaChu.OrderDishes.BLL.Constants;

namespace BuXiaChu.OrderDishes.Web.Admin.Restaurant
{
    public partial class Add : System.Web.UI.Page
    {
        protected List<AreaModel> cityList = null;
        protected List<ShareCodeModel> RestaurantCatList = null;
        protected string message = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                InitData();
            }
            else
            {
                Save();
            }
        }

        private void InitData()
        {
            #region
            cityList = AreaManager.GetListByParentId(0);
            foreach (var item in cityList)
            {
                ListItem li = new ListItem(item.Name, item.Id.ToString());
                sel_City.Items.Add(li);
            }

            RestaurantCatList = CodeDictionary.Instance.GetListByClassID(CodeDictionaryConstants.RestaurantCatID);
            foreach (var item in RestaurantCatList)
            {
                ListItem li = new ListItem(item.Name, item.CodeID.ToString());
                sel_Cat.Items.Add(li);
            }
            #endregion

            #region
            var restaurantId = 0;
            if (int.TryParse(Request.QueryString["id"], out restaurantId))
            {
                RestaurantModel entity = RestaurantManager.GetRestaurantById(restaurantId);
                sel_City.Value = entity.CityId.ToString();
                txt_AreaId.Value = entity.AreaId.ToString();
                txt_Name.Value = entity.Name;
                sel_Cat.Value = entity.Category.ToString();
                txt_Address.Value = entity.Address;
                txt_Tel.Value = entity.Telephone;
                txt_Tel1.Value = entity.Phone1;
                txt_Contact1.Value = entity.Contact1;
                txt_Tel2.Value = entity.Phone2;
                txt_Contact2.Value = entity.Contact2;
                txt_Website.Value = entity.Website;
                txt_Remarks.Value = entity.Remarks;
            }
            #endregion
        }

        private void Save()
        {
            #region
            var restaurantId = 0;
            int.TryParse(Request.QueryString["id"], out restaurantId);
            RestaurantModel entity = new RestaurantModel();
            entity.Id = restaurantId;
            entity.CityId = int.Parse(sel_City.Value);
            entity.AreaId = int.Parse(txt_AreaId.Value);
            entity.Name = txt_Name.Value.Trim();
            entity.Category = int.Parse(sel_Cat.Value);
            entity.Address = txt_Address.Value.Trim();
            entity.Telephone = txt_Tel.Value.Trim();
            entity.Phone1 = txt_Tel1.Value.Trim();
            entity.Contact1 = txt_Contact1.Value.Trim();
            entity.Phone2 = txt_Tel2.Value.Trim();
            entity.Contact2 = txt_Contact2.Value.Trim();
            entity.Website = txt_Website.Value.Trim();
            entity.Remarks = txt_Remarks.Value.Trim();
            if (RestaurantManager.Save(entity))
            {
                message = "ok";
            }
            else
            {
                message = "fail";
            }
            #endregion
        }
    }
}
